<template>
  <div>
    <!-- <headers /> -->
    <!-- <div style="padding-left: 30px;padding-top: 10px;">
            <el-breadcrumb separator="/" style="text-align: right;">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div> -->

    <div class="goodsindex">
      <!-- 搜索条件 -->
      <el-row :gutter="20" class="goodsindex-queryInfo">
        <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
          <el-input
            class="goodsindex-queryInfo-li"
            v-model="queryInfo.username"
            clearable
            size="small"
            placeholder="请输入用户名称"
          ></el-input>
        </el-col>
        <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
          <el-input
            class="goodsindex-queryInfo-li"
            v-model="queryInfo.jobNumber"
            clearable
            size="small"
            placeholder="请输入工号"
          ></el-input>
        </el-col>
        <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2">
          <el-button
            type="primary"
            class="goodsindex-queryInfo-li"
            size="small"
            @click="select"
            >搜索</el-button
          >
        </el-col>
        <el-col
          :xs="6"
          :sm="4"
          :md="3"
          :lg="2"
          :xl="2"
          v-if="isAuth('sys:user:save')"
        >
          <el-button
            type="primary"
            class="goodsindex-queryInfo-li"
            size="small"
            @click="handleEdit()"
            >添加
          </el-button>
        </el-col>
        <el-col
          :xs="6"
          :sm="4"
          :md="3"
          :lg="2"
          :xl="2"
          v-if="isAuth('sys:user:list')"
        >
          <el-button
            size="small"
            type="danger"
            class="goodsindex-queryInfo-li"
            @click="handleDelete()"
            :disabled="dataListSelections.length <= 0"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
      <!-- 检索结果 -->
      <el-row :gutter="20" class="goodsindex-list">
        <el-col :span="24">
          <el-table
            :data="userList"
            border
            :height="tableMaxHeight"
            size="small"
            @selection-change="selectionChangeHandle"
            style="width: 100%"
          >
            <el-table-column
              type="index"
              header-align="center"
              align="center"
              label="序"
              width="50"
            >
            </el-table-column>
            <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50"
            >
            </el-table-column>
            <el-table-column
              prop="username"
              header-align="center"
              align="center"
              label="用户名"
              min-width="180"
            >
            </el-table-column>
            <el-table-column
              prop="jobNumber"
              header-align="center"
              align="center"
              label="工号"
              min-width="180"
            >
            </el-table-column>
            <el-table-column
              prop="email"
              header-align="center"
              align="center"
              label="邮箱"
              min-width="180"
            >
            </el-table-column>
            <el-table-column
              prop="mobile"
              header-align="center"
              align="center"
              label="电话"
              min-width="180"
            >
            </el-table-column>
            <el-table-column
              prop="roleName"
              header-align="center"
              align="center"
              label="角色"
              min-width="180"
            >
            </el-table-column>
            <el-table-column
              width="180"
              prop="address"
              header-align="center"
              align="center"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  v-if="isAuth('sys:user:update')"
                  size="mini"
                  type="primary"
                  @click="handleEdit(scope.row.userId)"
                  >编辑</el-button
                >
                <el-button
                  v-if="isAuth('sys:user:delete')"
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row.userId)"
                  >删除</el-button
                >
                <!-- <el-button
							  size="mini"
							  type="primary"
							  @click="handleJurisdiction(scope.$index, scope.row)">菜单权限</el-button> -->
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- 分页 -->
      <el-row :gutter="20" class="goodsindex-list">
        <el-col :span="24" class="goodsindex-page-box">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageIndex"
            :page-sizes="[1, 10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalPage"
          >
          </el-pagination>
        </el-col>
      </el-row>
      <userAddorUpdata
        v-if="userAddorUpdataVisible"
        ref="userAddorUpdata"
        @getUserlist="getUserlist"
      >
      </userAddorUpdata>
    </div>
  </div>
</template>

<script>
import headers from "../../components/header/header.vue";
import userAddorUpdata from "./user/addorupdate.vue";
export default {
  components: {
    userAddorUpdata,
    headers,
  },
  data() {
    return {
      userAddorUpdataVisible: false, //新增 修改
      queryInfo: {
        username: "",
        jobNumber: "",
      },
      userList: [],
      dataListSelections: [], //批量删除
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      options: [
        {
          label: 1,
          value: "管理员",
        },
        {
          label: 2,
          value: "普通用户",
        },
      ],
    };
  },
  computed: {
    showOrHide() {
      // if(this.isAuth('')==false&&this.isAuth('')==false&&this.isAuth('')==false){
      //     this.isShowOrHide=false;
      // }
      return true;
    },
    tableMaxHeight() {
      return window.innerHeight - 180 + "px";
    },
  },
  created() {
    this.getUserlist();
  },
  methods: {
    //搜素
    async select() {
      this.pageIndex = 1;
      await this.getUserlist();
    },
    //获取用户列表
    getUserlist() {
      this.Sevice({
        url: "sys/user/list",
        method: "get",
        params: {
          username: this.queryInfo.username,
          jobNumber: this.queryInfo.jobNumber,
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
        },
      })
        .then((res) => {
          if (res && res.code == 200) {
            this.userList = res.page.list;
            this.totalPage = res.page.totalCount;
          } else {
            this.userList = [];
            this.totalPage = 0;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //修改
    handleEdit(userId) {
      this.userAddorUpdataVisible = true;
      this.$nextTick(() => {
        this.$refs.userAddorUpdata.init(userId);
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    //删除
    handleDelete(id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.userId;
          });
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.Sevice({
            url: "sys/user/delete",
            method: "post",
            data: userIds,
          })
            .then((res) => {
              if (res && res.code == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.getUserlist();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getUserlist();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getUserlist();
    },
  },
};
</script>

<style scoped>
.goodsindex {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}

/* 搜索 */
.goodsindex-queryInfo {
  margin-bottom: 10px;
}

.goodsindex-queryInfo-li {
  width: 100%;
  height: auto;
  margin-bottom: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 列表 */
.goodsindex-list {
  width: 100%;
  height: auto;
  margin-bottom: 20px;
}

/* 分页 */
.goodsindex-page-box {
  width: 100%;
  height: auto;
  display: flex;
  justify-content: flex-end;
}
</style>
